//
//  WBStatusDetailViewController.m
//  WeiBo
//
//  Created by bao on 2017/10/8.
//  Copyright © 2017年 powertorque. All rights reserved.
//

#import "WBStatusDetailViewController.h"

#import "WBStatusCommentsParameter.h"
#import "WBStatusCommentsResult.h"
#import "WBStatus.h"
#import "WBStatusCell.h"
#import "WBStatusFrame.h"
#import "WBStatusToolBar.h"
#import "WBStatusDetailView.h"
#import "WBStatusOriginalView.h"
#import "WBStatusRetweetedView.h"
#import "WBStatusPhotosView.h"
#import "WBTextDiyView.h"
#import "WBStatusDetailFrame.h"
#import "WBStatusPhoto.h"
#import "WBStatusDetailSectionHeaderView.h"
#import "WBLinkWebViewController.h"
#import "WBStatusComment.h"

#import "BSPhoto.h"
#import "BSPhotoBrowser.h"

@interface WBStatusDetailViewController () <UITableViewDataSource, UITableViewDelegate, WBStatusPhotosViewDelegate, WBStatusDetailViewDelegate, WBTextDiyViewDelegate, kWBStatusDetailSectionHeaderViewDelegate>

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, assign) WBStatusDetailSectionHeaderViewClickType currentType;

@property (nonatomic, strong) NSMutableArray<WBStatusComment *> *comments;
@property (nonatomic, strong) NSMutableArray *reweeteds;
@property (nonatomic, strong) NSMutableArray *praises;

@property (nonatomic, assign, getter=isAlreadyRequestComments) BOOL alreadyRequestComments;

@end

@implementation WBStatusDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadUI];
    
}

- (void)loadUI
{
    self.navigationItem.title = @"微博正文";
    
    // tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight) style:UITableViewStylePlain];
    tableView.backgroundColor = self.view.backgroundColor;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // tableHeaderView
    WBStatusFrame *statusFrame = [[WBStatusFrame alloc] init];
    statusFrame.status = self.status;
    
    CGFloat padding = 10.0f;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, statusFrame.cellHeight - kStatusToolBarHeight - kStatusSeperateLineHeight + padding * 2)];
    
    WBStatusCell *cell = [[WBStatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"status"];
    cell.backgroundColor = kStatusToTalBackgroundViewColor;
    cell.frame = CGRectMake(0, padding, kScreenWidth, statusFrame.cellHeight - kStatusToolBarHeight - kStatusSeperateLineHeight);
    cell.toolBar.hidden = YES;
    cell.line.hidden = YES;
    
    cell.detailView.originalView.photosView.delegate = self;
    cell.detailView.retweetedView.photosView.delegate = self;
    cell.detailView.originalView.textLabel.delegate = self;
    cell.detailView.retweetedView.retweetTextLabel.delegate = self;
    cell.detailView.delegate = self;
    cell.statusFrame = statusFrame;
    
    [bgView addSubview:cell];
    tableView.tableHeaderView = bgView;
    
    // toolbar
    WBStatusToolBar *toolbar = [WBStatusToolBar statusToolBar];
    toolbar.frame = CGRectMake(0, kScreenHeight - kTabBarHeight, kScreenWidth, kTabBarHeight);
    toolbar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:toolbar];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, toolbar.width, 0.5f)];
    line.backgroundColor = [UIColor lightGrayColor];
    [toolbar addSubview:line];
}

#pragma mark - Getter

- (NSMutableArray<WBStatusComment *> *)comments
{
    if (!_comments) {
        _comments = [NSMutableArray arrayWithCapacity:0];
    }
    return _comments;
}

- (NSMutableArray *)reweeteds
{
    if (!_reweeteds) {
        _reweeteds = [NSMutableArray arrayWithCapacity:0];
    }
    return _reweeteds;
}

- (NSMutableArray *)praises
{
    if (!_praises) {
        _praises = [NSMutableArray arrayWithCapacity:0];
    }
    return _praises;
}

#pragma mark - WBStatusPhotosViewDelegate

- (void)statusPhotosView:(WBStatusPhotosView *)statusPhotosView didClickCurrentIndex:(NSInteger)currentIndex
{
    NSMutableArray<BSPhoto *> *photos = [NSMutableArray array];
    for (int i = 0; i < statusPhotosView.pic_urls.count; i++) {
        BSPhoto *photo = [[BSPhoto alloc] init];
        photo.srcView = statusPhotosView.subviews[i];
        photo.remoteUrlString = [statusPhotosView.pic_urls[i].thumbnail_pic stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        [photos addObject:photo];
    }
    
    BSPhotoBrowser *browser = [BSPhotoBrowser browserWithPhotos:photos];
    browser.minimumLineSpacing = 10;
    browser.currentIndex = currentIndex;
    [browser showViewController:self];
}

#pragma mark - WBStatusDetailViewDelegate

- (void)statusDetailViewDidTapReweetedStatus:(WBStatusDetailView *)statusDetailView
{
    WBStatusDetailViewController *detailVC = [[WBStatusDetailViewController alloc] init];
    detailVC.status = statusDetailView.detailFrame.status.retweeted_status;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - WBTextDiyViewDelegate

- (void)textDiyView:(WBTextDiyView *)textDiyView didTapLink:(WBVisialLink *)link
{
    if (![self.navigationController.viewControllers.lastObject isKindOfClass:[self class]]) return;
    
    switch (link.linkResult.linkResultType) {
        case WBLinkResultType_Trend:
        {
            NSString *title = [NSString stringWithFormat:@"%ld--%@", link.linkResult.linkResultType, link.linkResult.string];
            UIViewController *vc = [[UIViewController alloc] init];
            vc.view.backgroundColor = [UIColor whiteColor];
            vc.navigationItem.title = title;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case WBLinkResultType_At:
        {
            NSString *title = [NSString stringWithFormat:@"%ld--%@", link.linkResult.linkResultType, link.linkResult.string];
            UIViewController *vc = [[UIViewController alloc] init];
            vc.view.backgroundColor = [UIColor whiteColor];
            vc.navigationItem.title = title;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case WBLinkResultType_Link:
        {
            WBLinkWebViewController *webVC = [[WBLinkWebViewController alloc] init];
            webVC.linkUrl = [link.linkResult.string copy];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Praise) {
        return self.praises.count;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Comment) {
        return self.comments.count;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Reweeted) {
        return self.reweeteds.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Praise) {
        static NSString *cellID = @"praise";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Comment) {
        static NSString *cellID = @"comment";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont systemFontOfSize:15];
        }
        cell.textLabel.text = self.comments[indexPath.row].text;
        return cell;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Reweeted) {
        static NSString *cellID = @"reweeted";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    } else {
        static NSString *cellID = @"others";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Praise) {
        return 44.0;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Comment) {
        return 35.0;
    } else if (self.currentType == WBStatusDetailSectionHeaderViewClickType_Reweeted) {
        return 44.0;
    } else {
        return 44.0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return kWBStatusDetailSectionHeaderViewHeight;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        static NSString *headerID = @"headerID";
        WBStatusDetailSectionHeaderView *sectionHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
        if (!sectionHeader) {
            sectionHeader = [[WBStatusDetailSectionHeaderView alloc] initWithReuseIdentifier:headerID];
            sectionHeader.frame = CGRectMake(0, 0, kScreenWidth, kWBStatusDetailSectionHeaderViewHeight);
            sectionHeader.contentView.backgroundColor = [UIColor whiteColor];
            sectionHeader.status = self.status;
            sectionHeader.delegate = self;
        }
        return sectionHeader;
    } else {
        return nil;
    }
}

#pragma mark - kWBStatusDetailSectionHeaderViewDelegate

- (void)statusDetailSectionHeaderView:(WBStatusDetailSectionHeaderView *)sectionHeaderView didClickButtonType:(WBStatusDetailSectionHeaderViewClickType)type
{
    self.currentType = type;
    switch (type) {
        case WBStatusDetailSectionHeaderViewClickType_Reweeted:
        {
            [self.tableView reloadData];
        }
            break;
        case WBStatusDetailSectionHeaderViewClickType_Comment:
        {
            if (self.isAlreadyRequestComments) {
                [self.tableView reloadData];
                return;
            }
            WBStatusCommentsParameter *parameter = [[WBStatusCommentsParameter alloc] init];
            parameter.statusId = self.status.idNum;
            [WBNetAPIBusiness statusComments:parameter completion:^(WBStatusCommentsResult *result, NSError *error) {
                self.alreadyRequestComments = YES;
                if (!error) {
                    [self.comments addObjectsFromArray:result.comments];
                    [self.tableView reloadData];
                }
            }];
        }
            break;
        case WBStatusDetailSectionHeaderViewClickType_Praise:
        {
            [self.tableView reloadData];
        }
            break;
        default:
            break;
    }
}

@end
